<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加学生</title>
    <link rel="stylesheet" href="../../css/common.css">
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <style>
        .txt {
            width: 300px;
        }
    </style>
</head>

<body>
    <form class="layui-form" lay-filter="test1">
        <div class="layui-form-item">
            <label class="layui-form-label">学号</label>
            <div class="layui-input-inline">
                <input type="text" name="studentNo" required lay-verify="required" placeholder="请输入学号"
                    class="layui-input txt">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-inline">
                <input type="password" name="loginPwd" required lay-verify="required" placeholder="请输入密码"
                    autocomplete="off" class="layui-input txt">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-inline">
                <input type="text" name="studentName" required lay-verify="required" placeholder="请输入学号"
                    class="layui-input txt">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="M" title="男">
                <input type="radio" name="sex" value="F" title="女" checked>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">年级</label>
            <div class="layui-input-inline">
                <select id="gradeId" name="gradeId" lay-verify="required">
                    <!-- 生成年级下拉框的模板 -->
                    <script id="gradeId_temp" type="text/html">
                    {{# layui.each(d, function(index, item){ }}
                    <option value="{{item.GradeId}}">{{item.GradeName}}</option>
                    {{#  }); }}
                    </script>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">电话</label>
            <div class="layui-input-inline">
                <input type="text" name="phone" required lay-verify="required" placeholder="请输入电话"
                    class="layui-input txt">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">地址</label>
            <div class="layui-input-block">
                <textarea name="address" placeholder="请输入地址" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">生日</label>
            <div class="layui-input-inline">
                <input type="text" id="bornDate" name="bornDate" required lay-verify="required" placeholder="请输入生日"
                    class="layui-input txt">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-inline">
                <input type="text" name="email" required lay-verify="required" placeholder="请输入邮箱"
                    class="layui-input txt">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">身份证</label>
            <div class="layui-input-inline">
                <input type="text" name="identityCard" required lay-verify="required" placeholder="请输入身份证号"
                    class="layui-input txt">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">添加</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
    <script src="../../layui/layui.js"></script>
    <script>
        layui.use(['form', 'laydate', 'laytpl'], function () {
            var laytpl = layui.laytpl;      //引入模板引擎
            var form = layui.form;          //引入表单模块
            var laydate = layui.laydate;    //引入日期模块
            $.get('http://www.bingjs.com:81/Grade/GetAll', data => {
                data.unshift({GradeId:0,GradeName:'请选择年级'})
                // 渲染模版
                laytpl($('#gradeId_temp').html()).render(data, function (html) {
                    $('#gradeId').html(html)
                })
                //更新渲染表单里面所有的select内容
                form.render('select', 'test1');
            })

            //执行一个laydate实例
            laydate.render({
                elem: '#bornDate' //指定元素
            });

            //监听提交
            form.on('submit(formDemo)', function (data) {
                console.log(data.field);
                $.post('http://www.bingjs.com:81/Student/Add', data.field, res => {
                    if (res) {
                        layer.msg('添加成功!')
                    } else {
                        layer.msg('添加失败!')
                    }
                    $('.layui-btn-primary').click()
                })
                // layer.msg(JSON.stringify(data.field));
                // $('.layui-form').submit()
                return false;
            });
        });
    </script>
</body>

</html>